package com.csk.zsk.compents.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.csk.zsk.commons.dao.impl.BaseHibernateDao;
import com.csk.zsk.compents.dao.RatingDao;
import com.csk.zsk.compents.entity.Rating;

@Repository
public class RatingDaoImpl extends BaseHibernateDao<Rating, String> implements RatingDao {

	@Override
	public String getRatingAVG(String objid) {
		String sql = "select avg(num) from Rating rating where rating.objid = '" + objid + "'";
		Object rltobj = createSQLQuery(sql).uniqueResult();
		if (rltobj != null) {
			String rlt = createSQLQuery(sql).uniqueResult().toString();
			return rlt;
		} else {
			return "0";
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public Rating getUserRating(String userid, String objid) {
		String sql = "from Rating rating where rating.userid=? and rating.objid=?";
		List<Rating> list = this.createQuery(sql, userid, objid).list();
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
}
